using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace TeleTwitter
{
    public partial class ToastForm : Form
    {
        private enum ToastState
        {
            Nothing,
            FadeUp,
            Visible,
            FadeDown
        }

        public event ToastTargetClickHandler ToastClick;

        private static bool _toastUp = false;
        private ToastState toastWindowState;
        private string _message;
        private object _toastTargetArgs;

        public ToastForm()
        {
            InitializeComponent();
        }

        private void ToastForm_Load(object sender, EventArgs e)
        {
            this.Left = SystemInformation.WorkingArea.Size.Width - this.Size.Width;
            this.Top = SystemInformation.WorkingArea.Size.Height - this.Size.Height;
            this.timerFadeIn.Enabled = true;
            this.toastWindowState = ToastState.FadeUp;
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            StringFormat f = new StringFormat();
            f.LineAlignment = StringAlignment.Center;

            e.Graphics.DrawString(this._message, this.Font, Brushes.Gray, new PointF(70, 21), f);
        }

        private void timerFadeIn_Tick(object sender, EventArgs e)
        {
            switch (this.toastWindowState)
            {
                case ToastState.FadeUp:
                    DoFadeIn();
                    break;
                case ToastState.Visible:
                    StartFadeOut();
                    break;
                case ToastState.FadeDown:
                    DoFadeDown();
                    break;
            }
        }
        private void DoFadeIn()
        {
            if (this.Opacity < 0.99)
            {
                this.Opacity += 0.05;
                this.Update();
            }
            else // Stop this timer and start a new one:
            {
                this.toastWindowState = ToastState.Visible;
                this.timerFadeIn.Interval = 3000;
            }
        }
        private void DoFadeDown()
        {
            if (this.Opacity > 0.01)
            {
                this.Opacity -= 0.05;
                this.Update();
            }
            else
            {
                this.Close();
                lock (typeof(ToastForm))
                    _toastUp = false;
            }
        }
        private void StartFadeOut()
        {
            this.timerFadeIn.Interval = 100;
            this.toastWindowState = ToastState.FadeDown;
        }
        private void StopFade()
        {
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F,FontStyle.Underline);
            this.timerFadeIn.Enabled = false;
            this.Opacity = 100;
        }
        private void StartFade()
        {
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F);
            this.Invalidate();
            this.StartFadeOut();
            this.timerFadeIn.Enabled = true;

        }
        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            if (ToastClick != null)
                ToastClick(this, new ToastSelectEventArgs(_toastTargetArgs));
            this.Close();
        }

        public static void ShowToast(string msg, object callbackArgs, ToastTargetClickHandler ToastLauncher)
        {
            lock (typeof(ToastForm))
            {
                if (_toastUp == false)
                {
                    ToastForm w = new ToastForm();
                    w._message = msg;
                    w._toastTargetArgs = callbackArgs;
                    if (ToastLauncher != null)
                        w.ToastClick += ToastLauncher;
                    w.Show();
                }
            }
        }

        private void ToastForm_MouseEnter(object sender, EventArgs e)
        {
            StopFade();
        }

        private void ToastForm_MouseLeave(object sender, EventArgs e)
        {
            StartFade();
        }
    }

    public delegate void ToastTargetClickHandler(object sender, ToastSelectEventArgs args);

    public class ToastSelectEventArgs : System.EventArgs
    {
        /// <summary>
        /// The object passed back when the user opens his toast.
        /// </summary>
        private readonly object _toastArgs;

        /// <summary>
        /// Retrieve the toast open arguments.
        /// </summary>
        public object ToastArgs
        {
            get
            {
                return _toastArgs;
            }
        }

        /// <summary>
        /// Single constructor.
        /// </summary>
        /// <param name="ToastArgs">The toast arguments.</param>
        public ToastSelectEventArgs(object ToastArgs)
        {
            _toastArgs = ToastArgs;
        }
    }

}